home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Swatch / Development / swatch 1.2 / swatch.prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-11  |  6.5 KB  |  274 lines  |  [TEXT/KAHL]

  1. /**
  2.  
  3.     swatch.prefs.c
  4.     Copyright (c) 1990, Adobe Systems, Inc.
  5.  
  6.  **/
  7.  
  8.  
  9. /**-----------------------------------------------------------------------------
  10.  **
  11.  **    Headers
  12.  **
  13.  **/
  14.  
  15. #include <Folders.h>
  16. #include "swatch.h"
  17. #include "swatch.prefs.h"
  18.  
  19.  
  20. /**-----------------------------------------------------------------------------
  21.  **
  22.  ** Private Variables
  23.  **
  24.  **/
  25.  
  26. char prefs_file_name[] = "\pSwatch Prefs";
  27. char prefs_temp_file_name[] = "\pSwatch |____temp";
  28. char riff_file_name[] = "\pSwatch Riff";
  29.  
  30. int16 prefs_VRefNum;
  31. int32 prefs_DirID;
  32.  
  33.  
  34. /**-----------------------------------------------------------------------------
  35.  **
  36.  ** Private Functions
  37.  **
  38.  **/
  39.  
  40. OSErr get_prefs_directory( void );
  41.  
  42. pascal OSErr FindFolder(short vRefNum,OSType folderType,Boolean createFolder,
  43.     short *foundVRefNum,long *foundDirID)
  44.     = {0x7000,0xA823}; 
  45.  
  46.  
  47. /*******************************************************************************
  48.  **
  49.  **    Public Variables
  50.  **
  51.  **/
  52.  
  53. Preferences_t Swatch_prefs;
  54.  
  55.  
  56. /*******************************************************************************
  57.  **
  58.  **    Public Functions
  59.  **
  60.  **/
  61.  
  62.  
  63. /*******************************************************************************
  64.  ***
  65.  *** prototype
  66.  ***
  67.  *** summary
  68.  ***
  69.  *** History:
  70.  ***
  71.  *** To Do:
  72.  ***
  73.  ***/
  74.  
  75. OSErr get_prefs_directory( void )
  76. {
  77.     if ( Pre_system_7 ) {
  78.         prefs_VRefNum = This_mac.sysVRefNum;
  79.         prefs_DirID = 0;
  80.         return noErr;
  81.     }
  82.     else
  83.         return FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  84.                 &prefs_VRefNum, &prefs_DirID );
  85. }
  86.  
  87.  
  88. void Read_prefs( void )
  89. {
  90.     HParamBlockRec read_PB;
  91.     Rect r;
  92.  
  93.     read_PB.ioParam.ioRefNum = 0;
  94.  
  95.     if ( get_prefs_directory() )
  96.         goto use_defaults;
  97.  
  98.     read_PB.ioParam.ioNamePtr = (StringPtr) prefs_file_name;
  99.     read_PB.ioParam.ioVRefNum = prefs_VRefNum;
  100.     read_PB.fileParam.ioDirID = prefs_DirID;
  101.     read_PB.ioParam.ioVersNum = 0;
  102.     read_PB.ioParam.ioPermssn = 0;
  103.     read_PB.ioParam.ioMisc = NULL;
  104.     if ( PBHOpen( &read_PB, FALSE ) )
  105.         goto use_defaults;
  106.  
  107.     read_PB.ioParam.ioBuffer = (Ptr) &Swatch_prefs;
  108.     read_PB.ioParam.ioReqCount = sizeof( Preferences_t );
  109.     read_PB.ioParam.ioPosMode = fsFromStart;
  110.     read_PB.ioParam.ioPosOffset = 0;
  111.     if ( PBRead( (ParamBlockRec *) &read_PB, FALSE ) )
  112.         goto use_defaults;
  113.  
  114.     if ( Swatch_prefs.version != PREFS_VERSION )
  115.         goto use_defaults;
  116.  
  117.     PBClose( (ParamBlockRec *) &read_PB, FALSE );
  118.     read_PB.ioParam.ioRefNum = 0;
  119.  
  120.     r = Swatch_prefs.window_rect;
  121.     r.top -= 18;
  122.     r.bottom = r.top + 18;
  123.     InsetRect( &r, 3, 3 );
  124.     if ( !RectInRgn( &r, *(RgnHandle *) GrayRgn ) )
  125.         goto use_default_window_rect;
  126.  
  127.     return;
  128.  
  129. use_defaults:
  130.     if ( read_PB.ioParam.ioRefNum )
  131.         PBClose( (ParamBlockRec *) &read_PB, FALSE );
  132.     Swatch_prefs.version = PREFS_VERSION;
  133.     Swatch_prefs.heap_scale = DEFAULT_HEAP_SCALE;
  134.     Swatch_prefs.heap_scale_2n = DEFAULT_HEAP_SCALE_2N;
  135. use_default_window_rect:
  136.     SetRect( &Swatch_prefs.window_rect, DEFAULT_WINDOW_LEFT, DEFAULT_WINDOW_TOP,
  137.             DEFAULT_WINDOW_RIGHT, DEFAULT_WINDOW_BOTTOM);
  138. }
  139.  
  140.  
  141. void Write_prefs( void )
  142. {
  143.     CInfoPBRec orig_cat_info_PB, new_cat_info_PB;
  144.     HParamBlockRec write_PB;
  145.     register Boolean previous_exists;
  146.  
  147.     write_PB.ioParam.ioRefNum = 0;
  148.  
  149.     if ( get_prefs_directory() )
  150.         goto cant_save;
  151.  
  152.     orig_cat_info_PB.hFileInfo.ioNamePtr = (StringPtr) prefs_file_name;
  153.     orig_cat_info_PB.hFileInfo.ioVRefNum = prefs_VRefNum;
  154.     orig_cat_info_PB.hFileInfo.ioDirID = prefs_DirID;
  155.     orig_cat_info_PB.hFileInfo.ioFDirIndex = 0;
  156.     if ( PBGetCatInfo( &orig_cat_info_PB, FALSE ) )
  157.         previous_exists = FALSE;
  158.     else
  159.         previous_exists = TRUE;
  160.  
  161.     write_PB.fileParam.ioNamePtr =
  162.             previous_exists ? (StringPtr) prefs_temp_file_name : (StringPtr) prefs_file_name;
  163.     write_PB.fileParam.ioVRefNum = prefs_VRefNum;
  164.     write_PB.fileParam.ioDirID = prefs_DirID;
  165.     write_PB.fileParam.ioFVersNum = 0;
  166.     PBHCreate( &write_PB, FALSE );
  167.  
  168.     write_PB.ioParam.ioVersNum = 0;
  169.     write_PB.ioParam.ioPermssn = 0;
  170.     write_PB.ioParam.ioMisc = NULL;
  171.     if ( PBHOpen( &write_PB, FALSE ) )
  172.         goto cant_save;
  173.  
  174.     write_PB.ioParam.ioBuffer = (Ptr) &Swatch_prefs;
  175.     write_PB.ioParam.ioReqCount = sizeof( Preferences_t );
  176.     write_PB.ioParam.ioPosMode = fsFromStart;
  177.     write_PB.ioParam.ioPosOffset = 0;
  178.     if ( PBWrite( (ParamBlockRec *) &write_PB, FALSE ) )
  179.         goto cant_save;
  180.  
  181.     PBClose( (ParamBlockRec *) &write_PB, FALSE );
  182.     write_PB.ioParam.ioRefNum = 0;
  183.  
  184.     if ( previous_exists ) {
  185.         write_PB.fileParam.ioNamePtr = (StringPtr) prefs_file_name;
  186.         write_PB.fileParam.ioDirID = prefs_DirID;
  187.         PBHDelete( &write_PB, FALSE );
  188.  
  189.         write_PB.fileParam.ioNamePtr = (StringPtr) prefs_temp_file_name;
  190.         write_PB.ioParam.ioMisc = (Ptr) prefs_file_name;
  191.         PBHRename( &write_PB, FALSE );
  192.     }
  193.  
  194.     new_cat_info_PB.hFileInfo.ioNamePtr = (StringPtr) prefs_file_name;
  195.     new_cat_info_PB.hFileInfo.ioVRefNum = prefs_VRefNum;
  196.     new_cat_info_PB.hFileInfo.ioDirID = prefs_DirID;
  197.     new_cat_info_PB.hFileInfo.ioFDirIndex = 0;
  198.     PBGetCatInfo( &new_cat_info_PB, FALSE );
  199.  
  200.     if ( previous_exists ) {
  201.         new_cat_info_PB.hFileInfo.ioFlFndrInfo = orig_cat_info_PB.hFileInfo.ioFlFndrInfo;
  202.         new_cat_info_PB.hFileInfo.ioFlXFndrInfo = orig_cat_info_PB.hFileInfo.ioFlXFndrInfo;
  203.         new_cat_info_PB.hFileInfo.ioFlCrDat = orig_cat_info_PB.hFileInfo.ioFlCrDat;
  204.     }
  205.     else {
  206.         new_cat_info_PB.hFileInfo.ioFlFndrInfo.fdType = 'PREF';
  207.         new_cat_info_PB.hFileInfo.ioFlFndrInfo.fdCreator = 'Peek';
  208.     }
  209.     new_cat_info_PB.hFileInfo.ioDirID = prefs_DirID;
  210.     PBSetCatInfo( &new_cat_info_PB, FALSE );
  211.     return;
  212.  
  213. cant_save:
  214.     if ( write_PB.ioParam.ioRefNum )
  215.         PBClose( (ParamBlockRec *) &write_PB, FALSE );
  216. }
  217.  
  218.  
  219. Handle Read_riff( void )
  220. {
  221.     HParamBlockRec read_PB;
  222.     Handle riff;
  223.     OSErr err;
  224.  
  225.     riff = 0;
  226.     read_PB.ioParam.ioRefNum = 0;
  227.  
  228.     if ( get_prefs_directory() )
  229.         goto no_riff;
  230.  
  231.     read_PB.ioParam.ioNamePtr = (StringPtr) riff_file_name;
  232.     read_PB.ioParam.ioVRefNum = prefs_VRefNum;
  233.     read_PB.fileParam.ioDirID = prefs_DirID;
  234.     read_PB.ioParam.ioVersNum = 0;
  235.     read_PB.ioParam.ioPermssn = 0;
  236.     read_PB.ioParam.ioMisc = NULL;
  237.     if ( PBHOpen( &read_PB, FALSE ) )
  238.         goto no_riff;
  239.  
  240.     if ( PBGetEOF( (ParamBlockRec *) &read_PB, FALSE ) )
  241.         goto no_riff;
  242.  
  243.     if ( !( riff = MFTempNewHandle( (Size) read_PB.ioParam.ioMisc, &err ) ) )
  244.         goto no_riff;
  245.     MFTempHLock( riff, &err );
  246.  
  247.     read_PB.ioParam.ioBuffer = (Ptr) StripAddress( *riff );
  248.     read_PB.ioParam.ioReqCount = (int32) read_PB.ioParam.ioMisc;
  249.     read_PB.ioParam.ioPosMode = fsFromStart;
  250.     read_PB.ioParam.ioPosOffset = 0;
  251.     if ( PBRead( (ParamBlockRec *) &read_PB, FALSE ) )
  252.         goto no_riff;
  253.  
  254.     PBClose( (ParamBlockRec *) &read_PB, FALSE );
  255.     return riff;
  256.  
  257. no_riff:
  258.     if ( read_PB.ioParam.ioRefNum )
  259.         PBClose( (ParamBlockRec *) &read_PB, FALSE );
  260.  
  261.     if ( riff )
  262.         MFTempDisposHandle( riff, &err );
  263.  
  264.     return NULL;
  265. }
  266.  
  267.  
  268. void Dispose_riff( Handle riff )
  269. {
  270.     OSErr err;
  271.  
  272.     MFTempDisposHandle( riff, &err );
  273. }
  274.